unit UInvoiceAdd;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, AdvPanel, cxGraphics,
  cxControls, cxLookAndFeels, cxLookAndFeelPainters, cxContainer, cxEdit,
  dxSkinsCore, dxSkinBlack, dxSkinBlue, dxSkinBlueprint, dxSkinCaramel,
  dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide, dxSkinDevExpressDarkStyle,
  dxSkinDevExpressStyle, dxSkinFoggy, dxSkinGlassOceans, dxSkinHighContrast,
  dxSkiniMaginary, dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky,
  dxSkinMcSkin, dxSkinMoneyTwins, dxSkinOffice2007Black, dxSkinOffice2007Blue,
  dxSkinOffice2007Green, dxSkinOffice2007Pink, dxSkinOffice2007Silver,
  dxSkinOffice2010Black, dxSkinOffice2010Blue, dxSkinOffice2010Silver,
  dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus,
  dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
  dxSkinTheAsphaltWorld, dxSkinsDefaultPainters, dxSkinValentine, dxSkinVS2010,
  dxSkinWhiteprint, dxSkinXmas2008Blue, cxGroupBox, DBAdvGlowNavigator,
  cxStyles, dxSkinscxPCPainter, cxCustomData, cxFilter, cxData, cxDataStorage,
  cxNavigator, Data.DB, cxDBData, cxGridLevel, cxClasses, cxGridCustomView,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGrid,
  cxTextEdit, cxMaskEdit, cxDropDownEdit, cxLookupEdit, cxDBLookupEdit,
  cxDBLookupComboBox, Vcl.StdCtrls, cxDBEdit, UDMInvoice, cxCalendar, cxSpinEdit,
  UDMPerson, UDMSettings, UDMItems, cxCheckBox, cxCurrencyEdit, ShellApi, cxGridExportLink, cxExport,
  frxClass, frxDBSet, ppComm, ppRelatv, ppDB, ppDBPipe, ppParameter, ppProd,
  ppClass, ppReport, ppCtrls, ppVar, ppPrnabl, ppBands, ppCache, ppDesignLayer,
  frxExportPDF;

type
  TFInvoiceAdd = class(TForm)
    AdvPanel1: TAdvPanel;
    AdvPanelStyler1: TAdvPanelStyler;
    cxGroupBox1: TcxGroupBox;
    AdvPanel2: TAdvPanel;
    DBAdvGlowNavigator1: TDBAdvGlowNavigator;
    cxGrid1DBTableView1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    Label1: TLabel;
    cxDBLookupComboBox1: TcxDBLookupComboBox;
    Label2: TLabel;
    cxDBDateEdit1: TcxDBDateEdit;
    Label3: TLabel;
    cxDBLookupComboBox2: TcxDBLookupComboBox;
    Label4: TLabel;
    cxDBLookupComboBox3: TcxDBLookupComboBox;
    Label5: TLabel;
    cxDBLookupComboBox4: TcxDBLookupComboBox;
    Label6: TLabel;
    cxDBSpinEdit1: TcxDBSpinEdit;
    Label7: TLabel;
    cxDBSpinEdit2: TcxDBSpinEdit;
    cxGrid1DBTableView1item_id: TcxGridDBColumn;
    cxGrid1DBTableView1coef_id: TcxGridDBColumn;
    cxGrid1DBTableView1coef_value: TcxGridDBColumn;
    cxGrid1DBTableView1qty: TcxGridDBColumn;
    cxGrid1DBTableView1category: TcxGridDBColumn;
    cxGrid1DBTableView1batch_date: TcxGridDBColumn;
    cxGrid1DBTableView1price_ins1: TcxGridDBColumn;
    cxGrid1DBTableView1price_ins2: TcxGridDBColumn;
    cxGrid1DBTableView1emergency: TcxGridDBColumn;
    cxGrid1DBTableView1private: TcxGridDBColumn;
    cxGrid1DBTableView1changed: TcxGridDBColumn;
    AdvPanel3: TAdvPanel;
    Label8: TLabel;
    cxDBCurrencyEdit1: TcxDBCurrencyEdit;
    Label9: TLabel;
    cxDBCurrencyEdit2: TcxDBCurrencyEdit;
    Label10: TLabel;
    cxDBCurrencyEdit3: TcxDBCurrencyEdit;
    DBAdvGlowNavigator2: TDBAdvGlowNavigator;
    Button1: TButton;
    DShis_encounter_invoice_details_sejour: TfrxDBDataset;
    DShis_encounter_invoice: TfrxDBDataset;
    ReInvoice: TfrxReport;
    DShis_encounter_all_info_all: TfrxDBDataset;
    DShis_encounter_invoice_details_bloc: TfrxDBDataset;
    DShis_encounter_invoice_details_divers: TfrxDBDataset;
    DShis_encounter_invoice_details_hono_details: TfrxDBDataset;
    DShis_encounter_invoice_details_lab: TfrxDBDataset;
    DShis_encounter_invoice_details_pharma: TfrxDBDataset;
    DShis_encounter_invoice_details_radio: TfrxDBDataset;
    frxPDFExport1: TfrxPDFExport;
    procedure DBAdvGlowNavigator1BtnCancel(Sender: TObject);
    procedure DBAdvGlowNavigator1BtnEdit(Sender: TObject);
    procedure DBAdvGlowNavigator1BtnFirst(Sender: TObject);
    procedure DBAdvGlowNavigator1BtnInsert(Sender: TObject);
    procedure DBAdvGlowNavigator1BtnLast(Sender: TObject);
    procedure DBAdvGlowNavigator1BtnNext(Sender: TObject);
    procedure DBAdvGlowNavigator1BtnPost(Sender: TObject);
    procedure DBAdvGlowNavigator1BtnPrior(Sender: TObject);
    procedure DBAdvGlowNavigator1BtnRefresh(Sender: TObject);
    procedure cxDBLookupComboBox1PropertiesEditValueChanged(Sender: TObject);
    procedure cxGrid1DBTableView1changedPropertiesEditValueChanged(
      Sender: TObject);
    procedure DBAdvGlowNavigator2BtnRefresh(Sender: TObject);
    procedure refreshCalculation();
    procedure refreshForm();
    procedure FormActivate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public

  end;

var
  FInvoiceAdd: TFInvoiceAdd;

implementation

{$R *.dfm}
procedure TFInvoiceAdd.refreshCalculation();
  var
    invoiceID : Integer;
    invoiceDetailId : Integer;
    opCode : Integer;
    encounterID : Integer;
    insuranceClass : Integer;
    insuranceNr : Integer;
    insuranceName : string;
    insuranceNr2 : Integer;
    insuranceName2 : string;
begin
  insuranceClass := DMInvoice.his_encounter_invoice.FieldByName('insurance_class_nr').Value;
  invoiceID := DMInvoice.his_encounter_invoice.FieldByName('his_encounter_invoice_id').Value;
  encounterID := DMInvoice.his_encounter_invoice.FieldByName('encounter_nr').Value;
  // GETTING INSURANCE NAME
  insuranceNr := DMInvoice.his_encounter_invoice.FieldByName('first_ins').Value;
  DMItems.his_tpa_class_combine_for_pricing.Locate('tpa_ins_class_combined_id',insuranceNr, []);
  insuranceName := DMItems.his_tpa_class_combine_for_pricing.FieldByName('ins_comb').Value;
  Self.cxGrid1DBTableView1price_ins1.Caption := insuranceName;
  if insuranceClass > 8 then
  begin
    insuranceNr2 := DMInvoice.his_encounter_invoice.FieldByName('second_ins').Value;
    DMItems.his_tpa_class_combine_for_pricing.Locate('tpa_ins_class_combined_id',insuranceNr2, []);
    insuranceName2 := DMItems.his_tpa_class_combine_for_pricing.FieldByName('ins_comb').Value;
    Self.cxGrid1DBTableView1price_ins2.Caption := insuranceName2;
  end;

  if insuranceClass = 1 then
  begin
    Self.cxGrid1DBTableView1price_ins2.Visible := False;
    DMInvoice.encounter_get_all_items.ParamByName('a').Value := encounterID;
    DMInvoice.encounter_get_all_items.ParamByName('b').Value := invoiceID;
    DMInvoice.ins_encounter_calculate_prices.ParamByName('a').Value := invoiceID;
    DMInvoice.ins_encounter_invoice_totals.ParamByName('a').Value := invoiceID;
    DMInvoice.encounter_get_all_items.Execute;
    DMInvoice.his_encounter_invoice_details.Refresh;
    DMInvoice.ins_encounter_calculate_prices.Execute;
    DMInvoice.his_encounter_invoice_details.Refresh;
    DMInvoice.ins_encounter_invoice_totals.Execute;
    DMInvoice.his_encounter_invoice.Refresh;
  end
  else if insuranceClass = 2 then
  begin
    Self.cxGrid1DBTableView1price_ins2.Visible := False;
    DMInvoice.encounter_get_all_items.ParamByName('a').Value := encounterID;
    DMInvoice.encounter_get_all_items.ParamByName('b').Value := invoiceID;
    DMInvoice.cnss_encounter_calculate_prices.ParamByName('a').Value := invoiceID;
    DMInvoice.cnss_encounter_invoice_totals.ParamByName('a').Value := invoiceID;
    DMInvoice.encounter_get_all_items.Execute;
    DMInvoice.his_encounter_invoice_details.Refresh;
    DMInvoice.cnss_encounter_calculate_prices.Execute;
    DMInvoice.his_encounter_invoice_details.Refresh;
    DMInvoice.cnss_encounter_invoice_totals.Execute;
    DMInvoice.his_encounter_invoice.Refresh;
  end
  else if insuranceClass = 3 then
  begin
    Self.cxGrid1DBTableView1price_ins2.Visible := False;
    DMInvoice.encounter_get_all_items.ParamByName('a').Value := encounterID;
    DMInvoice.encounter_get_all_items.ParamByName('b').Value := invoiceID;
    DMInvoice.coop_encounter_calculate_prices.ParamByName('a').Value := invoiceID;
    DMInvoice.coop_encounter_invoice_totals.ParamByName('a').Value := invoiceID;
    DMInvoice.encounter_get_all_items.Execute;
    DMInvoice.his_encounter_invoice_details.Refresh;
    DMInvoice.coop_encounter_calculate_prices.Execute;
    DMInvoice.his_encounter_invoice_details.Refresh;
    DMInvoice.coop_encounter_invoice_totals.Execute;
    DMInvoice.his_encounter_invoice.Refresh;
  end
  else if insuranceClass = 4 then
  begin
    Self.cxGrid1DBTableView1price_ins2.Visible := False;
    DMInvoice.encounter_get_all_items.ParamByName('a').Value := encounterID;
    DMInvoice.encounter_get_all_items.ParamByName('b').Value := invoiceID;
    DMInvoice.armee_encounter_calculate_prices.ParamByName('a').Value := invoiceID;
    DMInvoice.armee_encounter_invoice_totals.ParamByName('a').Value := invoiceID;
    DMInvoice.encounter_get_all_items.Execute;
    DMInvoice.his_encounter_invoice_details.Refresh;
    DMInvoice.armee_encounter_calculate_prices.Execute;
    DMInvoice.his_encounter_invoice_details.Refresh;
    DMInvoice.armee_encounter_invoice_totals.Execute;
    DMInvoice.his_encounter_invoice.Refresh;
  end
  else if insuranceClass = 6 then
  begin
    Self.cxGrid1DBTableView1price_ins2.Visible := False;
    DMInvoice.encounter_get_all_items.ParamByName('a').Value := encounterID;
    DMInvoice.encounter_get_all_items.ParamByName('b').Value := invoiceID;
    DMInvoice.mini_encounter_calculate_prices.ParamByName('a').Value := invoiceID;
    DMInvoice.mini_encounter_invoice_totals.ParamByName('a').Value := invoiceID;
    DMInvoice.encounter_get_all_items.Execute;
    DMInvoice.his_encounter_invoice_details.Refresh;
    DMInvoice.mini_encounter_calculate_prices.Execute;
    DMInvoice.his_encounter_invoice_details.Refresh;
    DMInvoice.mini_encounter_invoice_totals.Execute;
    DMInvoice.his_encounter_invoice.Refresh;
  end
  else if insuranceClass = 7 then
  begin
    Self.cxGrid1DBTableView1price_ins2.Visible := False;
    DMInvoice.encounter_get_all_items.ParamByName('a').Value := encounterID;
    DMInvoice.encounter_get_all_items.ParamByName('b').Value := invoiceID;
    DMInvoice.fsi_encounter_calculate_prices.ParamByName('a').Value := invoiceID;
    DMInvoice.fsi_encounter_invoice_totals.ParamByName('a').Value := invoiceID;
    DMInvoice.encounter_get_all_items.Execute;
    DMInvoice.his_encounter_invoice_details.Refresh;
    DMInvoice.fsi_encounter_calculate_prices.Execute;
    DMInvoice.his_encounter_invoice_details.Refresh;
    DMInvoice.fsi_encounter_invoice_totals.Execute;
    DMInvoice.his_encounter_invoice.Refresh;
  end
  else if insuranceClass = 8 then
  begin
    Self.cxGrid1DBTableView1price_ins2.Visible := False;
    DMInvoice.encounter_get_all_items.ParamByName('a').Value := encounterID;
    DMInvoice.encounter_get_all_items.ParamByName('b').Value := invoiceID;
    DMInvoice.muni_encounter_calculate_prices.ParamByName('a').Value := invoiceID;
    DMInvoice.muni_encounter_invoice_totals.ParamByName('a').Value := invoiceID;
    DMInvoice.encounter_get_all_items.Execute;
    DMInvoice.his_encounter_invoice_details.Refresh;
    DMInvoice.muni_encounter_calculate_prices.Execute;
    DMInvoice.his_encounter_invoice_details.Refresh;
    DMInvoice.muni_encounter_invoice_totals.Execute;
    DMInvoice.his_encounter_invoice.Refresh;
  end
  else if ((insuranceClass = 9) OR (insuranceClass = 11)  OR (insuranceClass = 12)  OR (insuranceClass = 13)) then
  begin
    Self.cxGrid1DBTableView1price_ins2.Visible := True;
    DMInvoice.encounter_get_all_items.ParamByName('a').Value := encounterID;
    DMInvoice.encounter_get_all_items.ParamByName('b').Value := invoiceID;
    DMInvoice.encounter_get_all_items.Execute;
    DMInvoice.his_encounter_invoice_details.Refresh;

    DMInvoice.ins_ins_encounter_calculate_prices.ParamByName('a').Value := invoiceID;
    DMInvoice.ins_ins_encounter_calculate_prices.ParamByName('b').Value := 0;
    DMInvoice.ins_ins_encounter_calculate_prices.Execute;
    DMInvoice.his_encounter_invoice_details.Refresh;

    DMInvoice.ins_ins_encounter_calculate_prices.ParamByName('a').Value := invoiceID;
    DMInvoice.ins_ins_encounter_calculate_prices.ParamByName('b').Value := 1;
    DMInvoice.ins_ins_encounter_calculate_prices.Execute;
    DMInvoice.his_encounter_invoice_details.Refresh;

    DMInvoice.ins_ins_encounter_invoice_totals.ParamByName('a').Value := invoiceID;
    DMInvoice.ins_ins_encounter_invoice_totals.Execute;
    DMInvoice.his_encounter_invoice.Refresh;
  end
  else if insuranceClass = 10 then
  begin
    Self.cxGrid1DBTableView1price_ins2.Visible := True;
    DMInvoice.encounter_get_all_items.ParamByName('a').Value := encounterID;
    DMInvoice.encounter_get_all_items.ParamByName('b').Value := invoiceID;
    DMInvoice.encounter_get_all_items.Execute;

    DMInvoice.ins_cnss_encounter_calculate_prices.ParamByName('a').Value := invoiceID;
    DMInvoice.ins_cnss_encounter_calculate_prices.ParamByName('b').Value := 0;
    DMInvoice.ins_cnss_encounter_calculate_prices.Execute;

    DMInvoice.ins_cnss_encounter_calculate_prices.ParamByName('a').Value := invoiceID;
    DMInvoice.ins_cnss_encounter_calculate_prices.ParamByName('b').Value := 1;
    DMInvoice.ins_cnss_encounter_calculate_prices.Execute;

    DMInvoice.ins_cnss_encounter_calculate_diffrence.ParamByName('a').Value := invoiceID;
    DMInvoice.ins_cnss_encounter_calculate_diffrence.Execute;

    DMInvoice.ins_cnss_encounter_invoice_totals.ParamByName('a').Value := invoiceID;
    DMInvoice.ins_cnss_encounter_invoice_totals.Execute;
    DMInvoice.his_encounter_invoice_details.Refresh;
    DMInvoice.his_encounter_invoice.Refresh;
  end;
end;

procedure TFInvoiceAdd.refreshForm;
  var billingType : Integer;
begin
  billingType := DMInvoice.his_encounter_invoice.FieldByName('insurance_class_nr').Value;
  if billingType > 8 then
  begin
    Self.Label4.Visible := True;
    Self.Label7.Visible := True;
    Self.cxDBLookupComboBox3.Visible := True;
    Self.cxDBSpinEdit2.Visible := True;
    Self.cxDBCurrencyEdit2.Visible := True;
    Self.Label9.Visible := True;
  end
  else
  begin
    Self.Label4.Visible := False;
    Self.Label7.Visible := False;
    Self.cxDBLookupComboBox3.Visible := False;
    Self.cxDBSpinEdit2.Visible := False;
    Self.cxDBCurrencyEdit2.Visible := False;
    Self.Label9.Visible := False;
  end;
end;

procedure TFInvoiceAdd.Button1Click(Sender: TObject);
begin
  DMInvoice.his_encounter_invoice_details_sejour.Refresh;
  DMInvoice.his_encounter_invoice_details_bloc.Refresh;
  DMInvoice.his_encounter_invoice_details_divers.Refresh;
  DMInvoice.his_encounter_invoice_details_hono_details.Refresh;
  DMInvoice.his_encounter_invoice_details_lab.Refresh;
  DMInvoice.his_encounter_invoice_details_pharma.Refresh;
  DMInvoice.his_encounter_invoice_details_radio.Refresh;
  Self.ReInvoice.ShowReport();
end;

procedure TFInvoiceAdd.cxDBLookupComboBox1PropertiesEditValueChanged(
  Sender: TObject);
  var billingType : Integer;
begin
  DMPerson.care_encounter.Locate('encounter_nr', Self.cxDBLookupComboBox1.EditValue,[loPartialKey]);
  DMInvoice.his_encounter_invoice.FieldByName('insurance_class_nr').Value := DMPerson.care_encounter.FieldByName('insurance_class_nr').Value;
  DMInvoice.his_encounter_invoice.FieldByName('first_ins').Value := DMPerson.care_encounter.FieldByName('insurance_firm_id').Value;
  DMInvoice.his_encounter_invoice.FieldByName('second_ins').Value := DMPerson.care_encounter.FieldByName('insurance_2_firm_id').Value;
  billingType := DMInvoice.his_encounter_invoice.FieldByName('insurance_class_nr').Value;
  if billingType > 8 then
  begin
    Self.Label4.Visible := True;
    Self.Label7.Visible := True;
    Self.cxDBLookupComboBox3.Visible := True;
    Self.cxDBSpinEdit2.Visible := True;
    Self.cxDBCurrencyEdit2.Visible := True;
    Self.Label9.Visible := True;
  end
  else
  begin
    Self.Label4.Visible := False;
    Self.Label7.Visible := False;
    Self.cxDBLookupComboBox3.Visible := False;
    Self.cxDBSpinEdit2.Visible := False;
    Self.cxDBCurrencyEdit2.Visible := False;
    Self.Label9.Visible := False;
  end;

//  if (billingType = 1) or (billingType = 4) or (billingType = 5) or (billingType = 7) then
//  begin
//    Self.cxDBSpinEdit1.Value := 100;
//    Self.cxDBSpinEdit2.Value := null;
//  end
//  else if (billingType = 2) or (billingType = 3) then
//  begin
//    Self.cxDBSpinEdit1.Value := 90;
//    Self.cxDBSpinEdit2.Value := null;
//  end
//  else if (billingType = 6) then
//  begin
//    Self.cxDBSpinEdit1.Value := 85;
//    Self.cxDBSpinEdit2.Value := null;
//  end
//  else if (billingType = 8) then
//  begin
//    Self.cxDBSpinEdit1.Value := 80;
//    Self.cxDBSpinEdit2.Value := null;
//  end
//  else if (billingType = 10) then
//  begin
//    Self.cxDBSpinEdit1.Value := 90;
//    Self.cxDBSpinEdit2.Value := 10;
//  end
//  else
//  begin
//    Self.cxDBSpinEdit1.Value := 0;
//    Self.cxDBSpinEdit2.Value := 0;
//  end;

end;

procedure TFInvoiceAdd.cxGrid1DBTableView1changedPropertiesEditValueChanged(
  Sender: TObject);
  var
    invoiceID : Integer;
    invoiceDetailId : Integer;
    opCode : Integer;
    encounterID : Integer;
    insuranceClass : Integer;
begin
  if DMInvoice.his_encounter_invoice_details.State =dsEdit then
  begin
    DMInvoice.his_encounter_invoice_details.Post;
  end;
  insuranceClass := DMInvoice.his_encounter_invoice.FieldByName('insurance_class_nr').Value;
  opCode := DMInvoice.his_encounter_invoice_details.FieldByName('changed').Value;
  invoiceID := DMInvoice.his_encounter_invoice_details.FieldByName('his_encounter_invoice_id').Value;
  invoiceDetailId := DMInvoice.his_encounter_invoice_details.FieldByName('his_encounter_invoice_details_id').Value;
  encounterID := DMInvoice.his_encounter_invoice.FieldByName('encounter_nr').Value;

  if insuranceClass = 1 then
  begin
    DMInvoice.ins_encounter_recalculate_item.ParamByName('a').Value := opCode;
    DMInvoice.ins_encounter_recalculate_item.ParamByName('b').Value := invoiceDetailId;
    DMInvoice.ins_encounter_recalculate_item.ParamByName('c').Value := invoiceID;
    DMInvoice.ins_encounter_recalculate_item.Execute;
    DMInvoice.his_encounter_invoice_details.Refresh;
    refreshCalculation();
  end
  else if insuranceClass = 2 then
  begin
    DMInvoice.cnss_encounter_recalculate_item.ParamByName('a').Value := opCode;
    DMInvoice.cnss_encounter_recalculate_item.ParamByName('b').Value := invoiceDetailId;
    DMInvoice.cnss_encounter_recalculate_item.ParamByName('c').Value := invoiceID;
    DMInvoice.cnss_encounter_recalculate_item.Execute;
    DMInvoice.his_encounter_invoice_details.Refresh;
    refreshCalculation();
  end
  else if insuranceClass = 3 then
  begin
    DMInvoice.coop_encounter_recalculate_item.ParamByName('a').Value := opCode;
    DMInvoice.coop_encounter_recalculate_item.ParamByName('b').Value := invoiceDetailId;
    DMInvoice.coop_encounter_recalculate_item.ParamByName('c').Value := invoiceID;
    DMInvoice.coop_encounter_recalculate_item.Execute;
    DMInvoice.his_encounter_invoice_details.Refresh;
    refreshCalculation();
  end
  else if insuranceClass = 4 then
  begin
    DMInvoice.armee_encounter_recalculate_item.ParamByName('a').Value := opCode;
    DMInvoice.armee_encounter_recalculate_item.ParamByName('b').Value := invoiceDetailId;
    DMInvoice.armee_encounter_recalculate_item.ParamByName('c').Value := invoiceID;
    DMInvoice.armee_encounter_recalculate_item.Execute;
    DMInvoice.his_encounter_invoice_details.Refresh;
    refreshCalculation();
  end
  else if insuranceClass = 6 then
  begin
    DMInvoice.mini_encounter_recalculate_item.ParamByName('a').Value := opCode;
    DMInvoice.mini_encounter_recalculate_item.ParamByName('b').Value := invoiceDetailId;
    DMInvoice.mini_encounter_recalculate_item.ParamByName('c').Value := invoiceID;
    DMInvoice.mini_encounter_recalculate_item.Execute;
    DMInvoice.his_encounter_invoice_details.Refresh;
    refreshCalculation();
  end
  else if insuranceClass = 7 then
  begin
    DMInvoice.fsi_encounter_recalculate_item.ParamByName('a').Value := opCode;
    DMInvoice.fsi_encounter_recalculate_item.ParamByName('b').Value := invoiceDetailId;
    DMInvoice.fsi_encounter_recalculate_item.ParamByName('c').Value := invoiceID;
    DMInvoice.fsi_encounter_recalculate_item.Execute;
    DMInvoice.his_encounter_invoice_details.Refresh;
    refreshCalculation();
  end
  else if insuranceClass = 8 then
  begin
    DMInvoice.muni_encounter_recalculate_item.ParamByName('a').Value := opCode;
    DMInvoice.muni_encounter_recalculate_item.ParamByName('b').Value := invoiceDetailId;
    DMInvoice.muni_encounter_recalculate_item.ParamByName('c').Value := invoiceID;
    DMInvoice.muni_encounter_recalculate_item.Execute;
    DMInvoice.his_encounter_invoice_details.Refresh;
    refreshCalculation();
  end
  else if ((insuranceClass = 9) OR (insuranceClass = 11)  OR (insuranceClass = 12)  OR (insuranceClass = 13)) then
  begin
    DMInvoice.ins_ins_encounter_recalculate_item.ParamByName('a').Value := opCode;
    DMInvoice.ins_ins_encounter_recalculate_item.ParamByName('b').Value := invoiceDetailId;
    DMInvoice.ins_ins_encounter_recalculate_item.ParamByName('c').Value := invoiceID;
    DMInvoice.ins_ins_encounter_recalculate_item.Execute;
    DMInvoice.his_encounter_invoice_details.Refresh;
    refreshCalculation();
  end
  else if insuranceClass = 10 then
  begin
    DMInvoice.ins_cnss_encounter_recalculate_item.ParamByName('a').Value := opCode;
    DMInvoice.ins_cnss_encounter_recalculate_item.ParamByName('b').Value := invoiceDetailId;
    DMInvoice.ins_cnss_encounter_recalculate_item.ParamByName('c').Value := invoiceID;
    DMInvoice.ins_cnss_encounter_recalculate_item.Execute;
    DMInvoice.his_encounter_invoice_details.Refresh;
    refreshCalculation();
  end;

end;

procedure TFInvoiceAdd.DBAdvGlowNavigator1BtnCancel(Sender: TObject);
begin
  Self.AdvPanel2.Enabled := false;
  DMInvoice.his_encounter_invoice.Cancel;
end;

procedure TFInvoiceAdd.DBAdvGlowNavigator1BtnEdit(Sender: TObject);
begin
  Self.AdvPanel2.Enabled := true;
  DMInvoice.his_encounter_invoice.Edit;
end;

procedure TFInvoiceAdd.DBAdvGlowNavigator1BtnFirst(Sender: TObject);
begin
  Self.AdvPanel2.Enabled := false;
  DMInvoice.his_encounter_invoice.First;
  refreshCalculation();
end;

procedure TFInvoiceAdd.DBAdvGlowNavigator1BtnInsert(Sender: TObject);
begin
  Self.AdvPanel2.Enabled := true;
  DMInvoice.his_encounter_invoice.Insert;
  refreshCalculation();
end;

procedure TFInvoiceAdd.DBAdvGlowNavigator1BtnLast(Sender: TObject);
begin
  Self.AdvPanel2.Enabled := false;
  DMInvoice.his_encounter_invoice.Last;
  refreshCalculation();
end;

procedure TFInvoiceAdd.DBAdvGlowNavigator1BtnNext(Sender: TObject);
begin
  Self.AdvPanel2.Enabled := false;
  DMInvoice.his_encounter_invoice.Next;
  refreshCalculation();
end;

procedure TFInvoiceAdd.DBAdvGlowNavigator1BtnPost(Sender: TObject);
begin
  Self.AdvPanel2.Enabled := true;
  DMInvoice.his_encounter_invoice.Post;
  DMInvoice.his_encounter_invoice.RefreshRecord;
  refreshCalculation();
  Self.AdvPanel2.Enabled := false;
end;

procedure TFInvoiceAdd.DBAdvGlowNavigator1BtnPrior(Sender: TObject);
begin
  Self.AdvPanel2.Enabled := false;
  DMInvoice.his_encounter_invoice.Prior;
  refreshCalculation();
end;

procedure TFInvoiceAdd.DBAdvGlowNavigator1BtnRefresh(Sender: TObject);
begin
  Self.AdvPanel2.Enabled := false;
  DMInvoice.his_encounter_invoice.RefreshRecord;
  refreshCalculation();
end;

procedure TFInvoiceAdd.DBAdvGlowNavigator2BtnRefresh(Sender: TObject);
begin
  DMInvoice.his_encounter_invoice_details.Refresh;
  refreshCalculation();
end;

procedure TFInvoiceAdd.FormActivate(Sender: TObject);
begin
  refreshForm();
  refreshCalculation();
end;

end.
